package com.ruoyi.web.controller.system;


import java.util.List;
import java.util.Set;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.yyyoupin.bean.CallBackInfoVo;
import com.ruoyi.cs.service.ITBackpackService;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
@SuppressWarnings("all")
public class SysLoginController
{
    private static final Logger log = LoggerFactory.getLogger(SysLoginController.class);

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ITBackpackService itBackpackService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @Log(title = "后台登录验证", businessType = BusinessType.INSERT)
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();

        if (StringUtils.isEmpty(loginBody.getUserPhone())) {
            return new AjaxResult(Constants.FAILCODE,"手机号不能为空！");
        }
        String cmsuserphone = sysDictDataMapper.selectDictLabel("CMSUSERPHONE", loginBody.getUserPhone());
        if (StringUtils.isEmpty(cmsuserphone)){
            return new AjaxResult(Constants.FAILCODE,"该手机号无操作权限！");
        }
//        //短信验证码前缀
//        String key = Constants.PHONE_CODE+loginBody.getUserPhone();
//        //从Redis获取短信验证码
//        String code1 = redisCache.getCacheObject(key);
//        if (StringUtils.isEmpty(code1) || !code1.equals(loginBody.getCodePhone())) {
//            return new AjaxResult(Constants.FAILCODE,"手机验证码错误!");
//        }
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid(),loginBody.getUserPhone());
        ajax.put(Constants.TOKEN, token);
//        redisCache.deleteObject(key);
        return ajax;
    }


    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }

//    /**饰品购买回执方法*/
//   @PostMapping("/buyNotify")
//   @Log(title = "饰品购买回执方法", businessType = BusinessType.UPDATE)
//   public String buyNotify(@RequestBody  NotifyBuyVo notifyBuyVo)
//    {
//        return itBackpackService.buyNotify(notifyBuyVo);
//    }

    /**饰品购买回执方法*/
    @PostMapping("/buyNotify")
    @Log(title = "饰品购买回执方法", businessType = BusinessType.UPDATE)
    public JSONObject buyNotify(@RequestBody CallBackInfoVo callBackInfoVo)
    {
        return itBackpackService.buyNotify(callBackInfoVo);
    }
}
